﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SD.Commons.DataFilters;
using System.Data;
using System.Globalization;
using SD.Business;

namespace SD.Web.Views.Orgranization
{
    public partial class ListOrganization : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var filter = new OrganizationFilter();
                ViewState["filter"] = filter;
                ViewState["currentLinkLetter"] = LinkButtonAll.UniqueID;
            }
        }

        protected void LinkLetter_Command(object sender, CommandEventArgs e)
        {
            // get id of previous link letter
            string previousLinkId = ViewState["currentLinkLetter"] as string;

            // check link letter is null or empty
            if (string.IsNullOrEmpty(previousLinkId) == false)
            {
                // find previous link letter control
                var previousLinkLetterControl = Master.FindControl(previousLinkId) as LinkButton;
                if (previousLinkLetterControl != null) // if it's found
                {
                    previousLinkLetterControl.CssClass = ""; // set class css is empty
                }
            }
            // get command name of current link letter
            string textOfLinkLetter = e.CommandName;

            // split commnad name of current link letter
            string[] letter = textOfLinkLetter.Split(new char[] { ' ', '-' }, StringSplitOptions.RemoveEmptyEntries);
            
            // get current link letter control
            LinkButton currentLinkLetterControl = sender as LinkButton;

            // set css class for current link letter control
            currentLinkLetterControl.CssClass = "hg-blue";

            // set value for viewstate currentLinkLetter
            ViewState["currentLinkLetter"] = currentLinkLetterControl.UniqueID;

            // get view state filter
            var filter = ViewState["filter"] as OrganizationFilter;
            if (filter != null)
            {
                filter.Letters = letter; // filter;
            }
            this.grvOrganization.DataBind();
        }

        /// <summary>
        /// Handles the Selecting event of the ObjectDataSourceOrganisation control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.ObjectDataSourceSelectingEventArgs"/> instance containing the event data.</param>
        protected void ObjectDataSourceOrganization_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            var filter = ViewState["filter"] as OrganizationFilter;
            e.InputParameters["filter"] = filter;
        }

        /// <summary>
        /// Handles the CheckedChanged event of the CheckBoxInactive control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void chkInactive_CheckedChanged(object sender, EventArgs e)
        {
            var filter = ViewState["filter"] as OrganizationFilter;
            if (filter != null)
            {
                filter.InActive = chkInactive.Checked;
            }
            grvOrganization.DataBind();
        }

        /// <summary>
        /// Handles the Command event of the LinkButtonDepartmentName control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
        protected void LinkButtonDepartmentName_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandArgument.Equals("False"))
            {
                int organizationId = int.Parse(e.CommandName, CultureInfo.CurrentCulture);
                OrganizationBUS.SetOrganizationStatus(organizationId, true);
                this.grvOrganization.DataBind();
            }

            //Response.Redirect(string.Format(CultureInfo.CurrentCulture, "~/View/Orgranization/Add.aspx?id={0}",
            //                                e.CommandName));            
        }

        /// <summary>
        /// show the status of Organization
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grvOrganization_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string statusOfOrg = DataBinder.Eval(e.Row.DataItem, "IsActive").ToString();
                if (Convert.ToBoolean(statusOfOrg)) // yes
                {
                    e.Row.Cells[4].Text = "Yes";
                }
                else
                {
                    e.Row.Cells[4].Text = "No";
                }
            }
        }


    }
}